package day8;

import java.io.*;
import java.util.ArrayList;

class Student implements Serializable{
    private static final long serialVersionUID = 1L;
    private String name;
    private Integer age;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
}

public class FileDemo9 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
//        1、定义一个存储Person对象的ArrayList集合
        ArrayList<Student> pList = new ArrayList<>();
//        2、往集合中存储person对象
        pList.add(new Student("张山峰",70));
        pList.add(new Student("赵义勇",20));
        pList.add(new Student("李敏",30));
//        3、创建一个序列化流ObjectOutputStream,对集合进行序列化
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E:\\vue\\yq_JAVA\\src\\main\\resources\\list.txt"));
//        4、使用ObjectOutputStream对象中的方法writeObject，对集合进行序列化
        oos.writeObject(pList);
//        5、创建一个反序列化流ObjectInputStream
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("E:\\vue\\yq_JAVA\\src\\main\\resources\\list.txt"));
//        6、使用ObjectInputStream对象中的方法readObject读取文件中保存的集合
        Object o = ois.readObject();
//        7、把Object类型的集合转换成ArrayList类型
        ArrayList<Student> list = (ArrayList<Student>)o;
//        8、遍历ArrayList结合
        for (Student student : list) {
            System.out.println(student);
        }
        ois.close();
        oos.close();
    }
}
